package br.ufrj.ppgi.jemf_advanced_example.mission;

import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ScrollView;
import br.ufrj.ppgi.jemf.mobile.bean.Mission;
import br.ufrj.ppgi.jemf.mobile.view.MissionView;
import br.ufrj.ppgi.jemf_advanced_example.BuildConfig;

/**
 * A fragment representing a single Mission detail screen.
 * This fragment is either contained in a {@link MissionListActivity} in two-pane mode (on tablets) or a {@link MissionDetailActivity} on handsets.
 */
public class MissionDetailFragment extends Fragment {
    /**
     * The serialization (saved instance state) Bundle key representing the activated item position.
     * Only used on tablets.
     */
    private static final String STATE_MISSION = "activated_mission";
	/**
     * The fragment argument representing the item ID that this fragment represents.
     */
    public static final String ARG_ID = "MISSION";
    /**
     * The Mission content this fragment is showing.
     */
    private static Mission mission;
    /**
     * The Mission view holder.
     */
    private static MissionView missionView;



    /**
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
     */
    public MissionDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the Mission content specified by the fragment arguments.
        if (getArguments().containsKey(MissionDetailFragment.ARG_ID)) {
            mission = getArguments().getParcelable(MissionDetailFragment.ARG_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	    // Show the Mission content
	    if (mission != null) {
	    	missionView = new MissionView(getActivity(), false);
	    	// Title
	    	missionView.setMissionTitleText(mission.getTitle());
	    	// Description
	    	missionView.setMissionDescriptionText(mission.getDescription());
	    	// Status
	    	missionView.setMissionStatusText(mission.getStatusString());
	    	// Start Date
	    	missionView.setMissionStartDateText(mission.getStartDateString());
	    	// End Date
	    	missionView.setMissionEndDateText(mission.getEndDateString());
	    	// Priority
	    	missionView.setMissionPriorityText(mission.getPriorityString());

	    	// Its necessary to add the MissionView inside a ScrollView to show scrolls
	    	ScrollView scrollView = new ScrollView(getActivity());
	    	scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	    	// Add Mission View as a child of the ScrollView.
	    	scrollView.addView(missionView);
	    	// Add a Tag to the view location
	    	scrollView.setTag(ARG_ID);

			return scrollView;
    	} else {
        	return null;
    	}
    }

    @Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
        // Restore the previously serialized activated Mission
        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_MISSION)) {
        	mission = savedInstanceState.getParcelable(STATE_MISSION);
        }
	}





	/**
     * Get updated Mission
     * @return Mission
     */
	public static Mission getMission() {
		if (BuildConfig.DEBUG) Log.i(MissionListActivity.TAG, "MissionDetailFragment ::: getMission");
    	return mission;
    }

	/**
	 * Update Mission data from UI components
	 */
	public static void updateDataFromUI() {
    	if ((mission != null) && (missionView != null)) {
    		if (BuildConfig.DEBUG) Log.i(MissionListActivity.TAG, "MissionDetailFragment ::: updateDataFromUI");
    		// Update Mission object with new data included by user through components
    		mission.setTitle(missionView.getMissionTitleText());
    		mission.setDescription(missionView.getMissionDescriptionText());
    		mission.setStatusString(missionView.getMissionStatusText());
    		mission.setStartDateString(missionView.getMissionStartDateText());
    		mission.setEndDateString(missionView.getMissionEndDateText());
    		mission.setPriorityString(missionView.getMissionPriorityText());
        }
    }



    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Force update data from UI
        updateDataFromUI();
        // Serialize and persist the activated Mission.
        outState.putParcelable(STATE_MISSION, mission);
    }
}
